package com.stone.serverless.web;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.gzip.GzipHandler;
import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description: TODO
 * @Author: MECHREVO
 * @Date: 2022/1/18 8:53
 * @Version: 1.0.0
 */
public class JettyServer {

    public ServletContext servletContext;

    private Server server;

    public void run(WebApplicationContext webApplicationContext) {
        server = new Server(8886);
        ContextHandlerCollection handlers = new ContextHandlerCollection();
        server.setHandler(handlers);

        //管理功能servlet配置
        ServletContextHandler manageHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
        manageHandler.setContextPath("/");
        servletContext = manageHandler.getServletContext();

        ServletHolder manageServletHolder = new ServletHolder(new DispatcherServlet(webApplicationContext));
        //设置上传文件大小限制
        manageServletHolder.getRegistration().setMultipartConfig(new MultipartConfigElement("data/tmp", 1024 * 1024 * 200, 1024 * 1024 * 200, 1024 * 1024 * 20));
        manageHandler.addServlet(manageServletHolder, "/*");
        ErrorPageErrorHandler errorMapper = new ErrorPageErrorHandler() {
            @Override
            public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
                if (response.getStatus() != HttpServletResponse.SC_NOT_FOUND) {
                    response.getWriter()
                            .append("{\"message\":\"HTTP error, error code is ")
                            .append(String.valueOf(response.getStatus()))
                            .append("\"}");
                } else {
                    super.handle(target, baseRequest, request, response);
                }
            }
        };
        errorMapper.addErrorPage(404, "/s.html");
        manageHandler.setErrorHandler(errorMapper);
        handlers.addHandler(manageHandler);

        //设置压缩
        GzipHandler gzipHandler = new GzipHandler();
        server.insertHandler(gzipHandler);




    }
    public void start(){
        new Thread(() -> {
            try {
                server.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }

    public void stop() {
        try {
            server.stop();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                server.stop();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        if (!server.isStopped()) {
            server.destroy();
        }

    }
}
